package m.jclecai.com.adapter.holder;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import m.jclecai.com.R;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnEditorAction;
import m.jclecai.com.animal.ExpandAnimation;
import m.jclecai.com.listener.Callback;

/**
 * Created by ZD on 2017/5/10.
 */

public class BonusOptimizationHolder extends ViewHolder {
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_bonus)
    TextView tvBonus;
    @BindView(R.id.include_multiple_cut)
    TextView includeMultipleCut;
    @BindView(R.id.include_multiple_context)
    EditText includeMultipleContext;
    @BindView(R.id.include_multiple_add)
    TextView includeMultipleAdd;
    @BindView(R.id.tv_tbonus)
    TextView tvTbonus;
    @BindView(R.id.llContainer)
    LinearLayout llContainer;
    int muli;
    float bonus;
    Callback changed;
    JSONObject obj;
    List<JSONObject> match;
    int mPosition;

    public BonusOptimizationHolder(Context context, @LayoutRes int resource) {
        super(context, resource);
    }

    public void init(JSONObject obj, Callback changed, List<JSONObject> match,int position){
        this.match = match;
        this.obj = obj;
        this.changed = changed;
        this.mPosition = position;
        muli = obj.getIntValue("muli");//倍数
        bonus = obj.getFloatValue("bonus");//单倍奖金
        String desc = obj.getString("desc").replaceAll("<br>","<br/>");
        tvTitle.setText(Html.fromHtml(desc));//名称
        includeMultipleContext.setText(String.valueOf(muli));
        tvTbonus.setText(obj.getString("tbonus"));//总奖金
        tvBonus.setText(String.valueOf(bonus));
        llContainer.setAnimation(null);
        llContainer.setVisibility(obj.get("select") != null ? VISIBLE : GONE);
        includeMultipleContext.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                if (TextUtils.isEmpty(charSequence.toString()))
                    return;
                includeMultipleContext.removeTextChangedListener(this);
                if (charSequence.toString().equals("0")) {
                    muli = 1;
                }else{
                    muli = Integer.parseInt(charSequence.toString());
                }
                includeMultipleContext.setText(muli+"");
                includeMultipleContext.addTextChangedListener(this);
                includeMultipleContext.setSelection(
                        charSequence.toString().trim().length());
//                changeBonus();
                return;
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }


        });
    }

    @OnClick(R.id.tv_title)
    void showDetail(){
        addItem(match,obj.getString("code"));
        llContainer.startAnimation(new ExpandAnimation(llContainer, 300,obj.get("select") == null));
        if(obj.get("select") != null)
            obj.put("select",null);
        else
            obj.put("select","select");
    }

    void addItem( List<JSONObject> match,String code){
        llContainer.removeAllViews();
        View inflate = View.inflate(getContext(), R.layout.item_bonus_optimiz_view, null);
        llContainer.addView(inflate);
        String[] c = obj.getString("desc").split("<br>");
        int index = 0;
        for (int i=0;i<match.size();i++){
            JSONObject obj = match.get(i);
            if(!code.contains(obj.getString("itemid"))) continue;
            inflate = View.inflate(getContext(), R.layout.item_bonus_optimiz_view, null);
            TextView one = (TextView)inflate.findViewById(R.id.tv_one);
            one.setText(obj.getString("name"));
            TextView two = (TextView)inflate.findViewById(R.id.tv_two);
            two.setText(obj.getString("hn"));
            TextView three = (TextView)inflate.findViewById(R.id.tv_three);
            three.setText(obj.getString("gn"));
            TextView four = (TextView)inflate.findViewById(R.id.tv_four);
            String f = obj.getString("sopt");
            if(c.length > index && !TextUtils.isEmpty(c[index])) {
                String[] split = c[index].split("=");
                if(split.length > 1) {
                    f = split[1];
                    index++;
                }
            }
            four.setText(f);
            llContainer.addView(inflate);
        }
    }

    @OnClick(R.id.include_multiple_cut)
    void cut(){
        muli = muli -1;
        if(muli < 1)
            muli = 1;
        includeMultipleContext.setText(String.valueOf(muli));
        changeBonus();
    }

    @OnClick(R.id.include_multiple_add)
    void add(){
        muli = muli +1;
        includeMultipleContext.setText(String.valueOf(muli));
        changeBonus();
    }

    void changeBonus(){
        tvTbonus.setText(String.valueOf(muli*bonus));
        obj.put("muli",muli);
        obj.put("tbonus",muli*bonus);
        if(changed!= null)
        changed.onClickSetChangedText();
    }

    @OnEditorAction(R.id.include_multiple_context)
    boolean onEditorAction(TextView v,int actionId,KeyEvent ev){
        if(actionId == EditorInfo.IME_ACTION_DONE){
            String s = includeMultipleContext.getText().toString();
            if(TextUtils.isEmpty(s))
                s = "1";
            muli = Integer.parseInt(s);
            includeMultipleContext.setText(s);
            changeBonus();
        }
        return false;
    }

    @OnClick(R.id.mRlOption)
    void delete(){
        changed.delete(mPosition);
    }
}
